// Audio.cpp : Defines the exported functions for the DLL application.
//

#include "Common.h"
#include "Audio.h"
#include "Headers.h"

using namespace Audio;
// This is an example of an exported variable
AUDIO_API int nAudio=0;

#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p);   (p)=NULL; } }
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }
#endif





AUDIO_API int TestFunction()
{
	return 42;
}


AUDIO_API bool Audio_Init()
{
	try	
	{		
		System::Init(new WindowsPlatform::WindowsFactory());
		IO::IOManager::Get()->Init(new IO::DefaultFileFactory());

		AudioManager::Get();				
	}
	catch (const ExceptionBase& e)
	{
		AudioManager::DestroyInstance();		
		return false;
	}
	return true;	
}

AUDIO_API void Audio_Destroy( )
{
	AudioManager::DestroyInstance();	
}

AUDIO_API void* Audio_CreateVoice(const wchar_t* fileName)
{
	if(AudioManager::Get())
	{
		return AudioManager::Get()->CreateVoice(fileName);
	}
	return NULL;
}

AUDIO_API void Audio_DestroyVoice(void* instance)
{
	if(AudioManager::Get())
	{
		//AudioManager::Get()->Stop();
		AudioManager::Get()->DestroyVoice((AudioVoice*)instance);
	}
}

AUDIO_API Audio::UINT32 Audio_Play(void* voice)
{
	if(AudioManager::Get())
	{
		return AudioManager::Get()->Play((AudioVoice*)voice);
	}
	return -1;
}


AUDIO_API void Audio_Stop(Audio::UINT32 playingId, BOOL destroyVoice/*= TRUE*/)
{
	if(AudioManager::Get())
	{
		return AudioManager::Get()->Stop(playingId);
	}
}

AUDIO_API void Audio_Resume(Audio::UINT32 playingId)
{
	if(AudioManager::Get())
	{
		return AudioManager::Get()->Resume(playingId);
	}
}

AUDIO_API int  Audio_GetSampleCount(void* instance)
{
	if(AudioManager::Get())
	{
		AudioVoice* voice = (AudioVoice*)instance;
		return voice->GetSampleCount();
	}
	return 0;
}

AUDIO_API int Audio_GetSamplePlayed(Audio::UINT32 playingId)
{
	if(AudioManager::Get())
	{
		AudioPlayingVoice* voice = AudioManager::Get()->GetPlayingVoiceById(playingId);
		if(voice != NULL)
		{
			return voice->GetSamplePlayed();
		}		
	}
	return 0;
}

AUDIO_API void Audio_SetVolume(Audio::UINT32 playingId, float volume)
{
	if(AudioManager::Get())
	{
		AudioPlayingVoice* voice = AudioManager::Get()->GetPlayingVoiceById(playingId);
		if(voice != NULL)
		{
			AudioManager::Get()->SendCommand(new SetVolumeCommand(voice, volume));
		}
	}
}

AUDIO_API Audio::UINT32  Audio_GetProgress(Audio::UINT32 playingId)
{
	if(AudioManager::Get())
	{
		AudioPlayingVoice* voice = AudioManager::Get()->GetPlayingVoiceById(playingId);
		if(voice != NULL)
		{
			return voice->GetSamplePlayed();
		}		
	}
	return 0;
}

// This is the constructor of a class that has been exported.
// see Audio.h for the class definition
CAudio::CAudio()
{
	return;
}
